-- TransEliminate
-- Created by cheny3 Aug/10/2016
-- 转换消除

-- 初始化
function initTransEliminate(self)
    self.item:removeAllChildren();

    -- 位置
    local pos = self.gridData:getPos();

    local function onClicked(sender, eventType)
        -- 以逻辑层的数据为准
        local grid = DungeonM.getGridByPos(pos);
        local transId = grid.trans_id;
        local monsterId = TransEliminateM.query(transId, "monster");

        local function longPressCallback()
            if not monsterId then
                return;
            end

            local extraPara = {
                ["baseValue"] = {getLocStr("tower_attrib_fault"), getLocStr("tower_attrib_fault"),
                    getLocStr("tower_attrib_fault"), getLocStr("tower_attrib_fault")},
            };

            -- 显示怪物信息界面
            local uiInfo = UIMonsterInfo.create(monsterId, nil, extraPara);
            UIMgr.getCurrentScene():addForm(uiInfo);
        end

        -- 处理长按
        if handleLongPress(sender, eventType, longPressCallback) then
            return;
        end

        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 转换元素
            DungeonActionM.go("trans_eliminate", pos);
        end
    end

    local transId = self.gridData.trans_id;
    local monsterId = TransEliminateM.query(transId, "monster");

    -- 创建模型
    ----////TODO 可能会卡
    local display = self:createMonsterDisplay(monsterId);
    self.item.transDisplay = display;
    self.item:addChild(display);
    display:switchAction(MONSTER_ACTION_IDLE);

    -- 用一张透明的图片来接收点击事件
    local iconPath = getLevelItemIconPath("default");
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setOpacity(0);
    self.icon:setLocalZOrder(100);

    self.item:setPositionY(10);
end

-- 转换元素
function onTransEliminate(self)
    local pos = self.gridData:getPos();

    -- 以逻辑层的数据为准
    local grid = DungeonM.getGridByPos(pos);
    local transId = grid.trans_id;
    local monsterId = TransEliminateM.query(transId, "monster");

    self.item:removeChild(self.item.transDisplay);
    self.item.transDisplay = nil;

    local display = self:createMonsterDisplay(monsterId);
    self.item.transDisplay = display;
    self.item:addChild(display);
    display:switchAction(MONSTER_ACTION_IDLE);

    -- debuff光效和光圈
    playEffect(self.item, 1183);
    playEffect(self.item, 1230);
end